package 数据结构OJ;


public class 删除有序链表中重复元素节点_05 {
    private static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }

        public ListNode deleteDuplication(ListNode pHead) {


            if (pHead==null || pHead.next==null){return pHead;}
            ListNode Head = new ListNode(0);
            Head.next = pHead;
            //前链表节点
            ListNode pre  = Head;
            //工作节点
            ListNode last = Head.next;
            while (last!=null){
                if(last.next!=null && last.val == last.next.val){
                    // 找到最后的一个相同节点
                    while (last.next!=null && last.val == last.next.val){
                        last = last.next;
                    }
                    pre.next = last.next;
                    last = last.next;
                }else{
                    pre = pre.next;
                    last = last.next;
                }
            }
            return Head.next;





//            ListNode first = new ListNode(-1);//设置一个trick
//            first.next = pHead;
//
//            ListNode p = pHead;
//            ListNode last = first;
//            while (p != null && p.next != null) {
//                if (p.val == p.next.val) {
//                    int val = p.val;
//                    while (p != null && p.val == val)
//                        p = p.next;
//                    last.next = p;//删除重复结点
//                } else {
//                    last = p;
//                    p = p.next;
//                }
//            }
//            return first.next;

        }
    }



}


